﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace MLY.Admin
{
  public partial class editOrg : Bll.BasePage
  {
    MLY.Bll.clsOrganization org;

    protected void Page_Load(object sender, EventArgs e)
    {
      Dal.lqKbmDataContext dc = new MLY.Dal.lqKbmDataContext();
      if (!Page.IsPostBack)
      {

        Response.Expires = 0;
        ViewState["Referer"] = Request.ServerVariables["HTTP_REFERER"];
        int orgId = int.Parse(Request.QueryString["Edit"].ToString());
        if (Int32.TryParse(Request.QueryString["Edit"], out orgId))
        {
          if (orgId != -1)
          {

            org = new MLY.Bll.clsOrganization(orgId);
            this.txtName.Text = org.Data.OrganizationName.ToString();
            this.ddlPayMentType.SelectedValue = org.Data.PaymentTypeId.ToString();
            this.txtTag.Text = org.Data.Tag;
            this.txtAddress.Text = org.Data.Address;
            this.txtContactEMail.Text = org.Data.ContactPersonEmail;
            this.txtContactPhone.Text = org.Data.ContactPersonPhone;
            this.txtContactPerson.Text = org.Data.ContactPerson;
            this.txtWebSite.Text = org.Data.WebSite;
            this.CheckBoxClient.Checked = (bool)org.Data.IsClient;
            this.CheckBoxContractor.Checked = (bool)org.Data.IsContractor;
            this.classificationDdl.SelectedValue = dc.TableOrganizationClassifications.Where(S => S.OrganizationId == orgId)
                .Select(S => S.ClassificationId).FirstOrDefault().ToString();
          }

        }
      }
    }

    protected void btnOk_Click(object sender, ImageClickEventArgs e)
    {


      Dal.lqKbmDataContext dc = new MLY.Dal.lqKbmDataContext();
      int orgId = int.Parse(Request.QueryString["Edit"].ToString());
      Bll.clsOrganizationClassification Orgclass;
      if (orgId != -1)
      {
        org = new MLY.Bll.clsOrganization(orgId);

      }
      else
      {
        org = new MLY.Bll.clsOrganization();
      }

      org.Data.OrganizationName = this.txtName.Text;
      //  organization.Data.PaymentTypeId = int.Parse(this.ddlPayMentType.SelectedValue);

      org.Data.Tag = this.txtTag.Text;
      org.Data.Address = this.txtAddress.Text;
      org.Data.ContactPersonEmail = this.txtContactEMail.Text;
      org.Data.ContactPersonPhone = this.txtContactPhone.Text;
      org.Data.ContactPerson = this.txtContactPerson.Text;
      org.Data.WebSite = this.txtWebSite.Text;
      org.Data.IsClient = this.CheckBoxClient.Checked;
      org.Data.IsContractor = this.CheckBoxContractor.Checked;
      org.Data.PaymentTypeId = dc.LookupPaymentTypes.Where(S => S.PaymentTypeId == int.Parse(this.ddlPayMentType.SelectedValue)).Select(s => s.PaymentTypeId).FirstOrDefault();

      //organization.Data.PaymentTypeId = int.Parse(this.ddlPayMentType.SelectedValue);

      org.Save();
      Orgclass = new MLY.Bll.clsOrganizationClassification(orgId == -1 ? org.Data.OrganizationId : orgId);
      Orgclass.Data.OrganizationId = org.Data.OrganizationId;
      Orgclass.Data.ClassificationId = int.Parse(this.classificationDdl.SelectedValue);
      Orgclass.Save();
      Response.RedirectTo((string)ViewState["Referer"]);
    }

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
      Response.RedirectTo((string)ViewState["Referer"]);

    }

    public void DeleteOrg(int OrgId)
    {

    }
  }


}
